package main

import (
	"fmt"
	"log"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "admin"
	password = "admin"
	dbname   = "test"
)

type Product struct {
	gorm.Model
	Code  string
	Price uint
}

func main() {
	//http://gorm.io/docs/index.html
	//https://www.kancloud.cn/kancloud/xorm-manual-zh-cn/
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
	db, err := gorm.Open("postgres", psqlInfo)
	defer db.Close()

	if err != nil {
		log.Panicln(err)
	} else {
		log.Println("fasf")
	}

	// 自动迁移模式
	//	db.AutoMigrate(&Product{})
	db.LogMode(true)
	// 创建
	db.Create(&Product{Code: "干扰器恩阳区", Price: 34500})

	// 读取
	var product Product
	db.First(&product, 1) // 查询id为1的product
	log.Println(product)
	//	db.First(&product, "code = ?", "L1212") // 查询code为l1212的product

	// 更新 - 更新product的price为2000
	//	db.Model(&product).Update("Price", 2000)

	// 删除 - 删除product
	//	db.Delete(&product)

}
